import requests
from bs4 import BeautifulSoup

from api.login_register import LoginRegister
from config import URLPATH


class OpenAccount:
    # 实名认证
    def real_name_authentication(self, session, realname, card_id):
        # 说明：当前接口需要处理的请求体数据类型为多消息数据（设计文件文件上传）
        #      但是当前接口没有实现真正的图片或文件上传功能，测试时需要给一个假数据即可
        return session.post(url=f'{URLPATH}/member/realname/approverealname',
                          data={'realname':realname, 'card_id':card_id},
                            files={'key':'value'})
                          # 若此处不加files,这不会返回正确的结果

    # 获取认证信息
    def get_real_name_info(self, session):
        return session.post(url=f'{URLPATH}/member/member/getapprove')

    # 开户
    def open_account(self, session):
        return session.post(url=f'{URLPATH}/trust/trust/register')

    # 第三方开户接口,使用BeautifulSoup解析HTML
    def open_account_third(self, session, url, data):
        return session.post(url=url,data=data)

if __name__ == '__main__':
    open_account = OpenAccount()
    lr = LoginRegister()
    session = requests.session()
    print('-'*200)
    print(lr.login_data(session, '18888888888', '123456').text)
    print('-' * 200)
    print(open_account.real_name_authentication(session,'小米','123456789012345678').text)
    print('-' * 200)
    print(open_account.get_real_name_info(session).text)
    print('-' * 200)
    print(open_account.open_account(session).json())
    print('-' * 200)

    # 获取第三方的开户信息,使用BeautifulSoup解析HTML
    html_data = open_account.open_account(session).json().get("description").get('form')
    print(html_data)
    # html.parser:为HTML的解析器
    soup = BeautifulSoup(html_data, 'html.parser')
    # 获取form标签中method属性的值
    print(soup.form['method'])
    # 获取form标签中action属性的值
    url = soup.form['action']
    print(soup.form['action'])
    # 获取所有input标签中name属性的值以及value属性的值,并且保存在字典中
    json_data= {}
    for input_tag in soup.find_all('input'):
        json_data[input_tag['name']] = input_tag['value']
    print(json_data)
    print(open_account.open_account_third(session,url,json_data).text)


